package com.bracketbird.client.url;

import com.bracketbird.client.*;
import com.bracketbird.client.model.keys.*;
import com.bracketbird.clientcore.appcontrol.*;
import com.bracketbird.clientcore.model.keys.*;
import com.bracketbird.clientcore.util.*;

import java.util.*;

/**
 *
 */
public class UrlCommand {

    private UrlParam urlParam;
    private String baseUrl;

    public static String URL_COMMAND = "URL_COMMAND";

    private static Map<String, Command> commands = new HashMap<String, Command>();

    static {
        commands.put(EditTournament.name, new EditTournament());
        commands.put(DeleteAccount.name, new DeleteAccount());
        commands.put(DeleteMembership.name, new DeleteMembership());
    }



    //FACTORY METHODS
    public static UrlCommand createEditTournament(String baseUrl, EditTournament command, ClubId cId, TournamentId tId){
        UrlParam up = getUrlParam(command);
        up.add(UrlParam.CLUB_ID, cId.getEncodedKey());
        up.add(UrlParam.TOURNAMENT_ID, tId.getEncodedKey());
        up.add(UrlParam.USER_ID, UserManager.getInstance().getUser().getId().getEncodedKey());
        up.add(UrlParam.INVALID_DATE, String.valueOf(DateUtil.addHoursToDate(new Date(), 1).getTime()));
        return new UrlCommand(baseUrl, up);
    }

    public static UrlCommand deleteAccount(String baseUrl, DeleteAccount command, UserId userId){
        UrlParam up = getUrlParam(command);
        up.add(UrlParam.USER_ID, userId.getEncodedKey());
        return new UrlCommand(baseUrl, up);
    }

    public static UrlCommand deleteMembership(String baseUrl, DeleteMembership command, MemberId memberId, String clubName){
        UrlParam up = getUrlParam(command);
        up.add(UrlParam.MEMBER_ID, memberId.getEncodedKey());
        up.add(UrlParam.CLUB_NAME, clubName);
        up.add(UrlParam.CREATED_DATE, String.valueOf(new Date().getTime()));
        return new UrlCommand(baseUrl, up);
    }
    






    private static UrlParam getUrlParam(Command c){
        UrlParam up = new UrlParam();
        up.add(URL_COMMAND, c.getName());
        return up;
    }



    public UrlCommand(String baseUrl, UrlParam urlParam) {
        this.baseUrl = baseUrl+"#";
        this.urlParam = urlParam;

    }

    public String getUrl(){
        return baseUrl+urlParam.toString();
    }

    public String toString(){
        return getUrl();
    }

    public static void execute(Map<String, String> pMap) {
        Command c = commands.get(pMap.get(URL_COMMAND));
        if(c == null){
            throw new SystemException("Command object not setup for "+pMap.get(URL_COMMAND));
        }
        c.execute(pMap);
    }
}
